<?php
session_start();
if(!ob_start("ob_gzhandler")) ob_start();
?>
<html>
	<head>
		<title></title>
		<link rel = "stylesheet" type = "text/css" href = "mainFrame.css">
		<link rel = 'stylesheet' type = 'text/css' href = 'jquery-ui-1.8.18.custom.css'>
		<script type='text/javascript' src='jquery-1.7.js'></script>
		<script type = 'text/javascript' src = 'jquery-ui-1.8.16.custom.min.js'></script>
		<script type = "text/javascript" src = "mainFrameScript.js"></script>
		<link rel = "stylesheet" type = "text/css" href = "customNonJqueryDialog.css">
	</head>
		<body>
		<?php
	mysql_connect("localhost", "sapmenet_admin", "a1s2d3f4g5h6j7") or die ("Problem with datebase");
	mysql_select_db("sapmenet_users");
	if(isset($_POST['zaek']))
	{	
		if ($_POST['zaek']==1)
		{
			$all=$_POST['zaek'];
		}
		else
		{
			$all=0;
		}
	}
	else
	{
		$all=0;
	}
	if($all==0)
	{
	
	if (isset($_SESSION['JID']))
	{
		$userID=$_SESSION['JID'];
		$currentID=$_SESSION['id'];
		if ($userID!=$currentID)
		{
			$id=$_SESSION['id'];
	
			$publication = "SELECT publications.text, publications.date, publications.numberOfLikes, users.ID, users.FirstName, users.LastName, publications.publisherID, publications.numberOfDislikes, publications.ID  FROM publications JOIN users ON publications.publisherID=users.ID WHERE publications.userID = $id order by publications.ID desc LIMIT 0, 30";
			$publication= mysql_query($publication);
			//$idPublication = "SELECT ID FROM publications WHERE currentID = $id";
			//$idPublication = mysql_query($idPublication);
			while($thePublication = mysql_fetch_array($publication, MYSQL_NUM))
			{
				echo ("<br>");
				echo ("<div class='publication'><span class='publisher'><a href='http://sapme.net/Framework/profilePage.php?id=".$thePublication['6']."'>".$thePublication['4']." ".$thePublication['5'].":</a></span><br><p id='parPub'>".$thePublication[0]."</p>  <br><span id='date'>Date: ". $thePublication['1']." </span> &nbsp;&nbsp; <img src = 'like.png' class = 'likeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Like this'> Likes: ". $thePublication['2']."&nbsp;&nbsp;&nbsp; <img src = 'dislike.png' class = 'DislikeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Dislike this'> Dislikes: ".$thePublication['7']."<span class = 'showPublicationCommentsButton' id = '".$thePublication[8]."'>Comments</span>");
				echo ("</div>");			
	
	
			}
	echo "<button id='allPublications'>Show all publications</button>";
		}
	else{
	$id=$_SESSION['id'];
	
	
	$publication = "SELECT publications.text, publications.date, publications.numberOfLikes, users.ID, users.FirstName, users.LastName, publications.publisherID,  publications.numberOfDislikes, publications.ID FROM publications JOIN users ON publications.publisherID=users.ID WHERE publications.userID = $id order by publications.ID desc LIMIT 0, 30";
	$publication= mysql_query($publication);
	while($thePublication = mysql_fetch_array($publication, MYSQL_NUM))
	{
		echo ("<br>");
		echo ("<div class='publication' id='".$thePublication[8]."'><span class='publisher'><a href='http://sapme.net/Framework/profilePage.php?id=".$thePublication['6']."'>".$thePublication['4']." ".$thePublication['5'].":</a><img src='Icons/close.png' class='deletePub' id='".$thePublication[8]."'></span><br><p id='parPub'>".$thePublication[0]."</p>  <br><span id='date'>Date: ". $thePublication['1']." </span> &nbsp;&nbsp;<img src = 'like.png' class = 'likeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Like this'> Likes: ". $thePublication['2']."&nbsp;&nbsp;&nbsp; <img src = 'dislike.png' class = 'DislikeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Dislike this'> Dislikes: ".$thePublication['7']."<span class = 'showPublicationCommentsButton' id = '".$thePublication[8]."'>Comments</span>");                                                                 
		echo ("</div>");			
	}
	echo "<button id='allPublications'>Show all publications!</button>";
	mysql_query("UPDATE publications SET seen = 1 WHERE userID = {$_SESSION['JID']}");
	}
	echo"<div id='theFiuuuRotateTransformingCommentsDiv' class = 'dialogBody'>
			<div class = 'dialogHeader'>
				<h3>Comments</h3>
				<img src = 'Icons/close.png' class = 'dialogCloseButton' id = 'closePubComment'>
			</div>
			<div class = 'dialogHeaderLess' id = 'theFiuuuRotateTransformingCommentsDivHeaderless'>
			</div>
		</div>";
	}
	else{
		header ('Location: ..\index.php');
		exit();
	}
	}
	//ot tyk vsi4ki poyblikacii
	else
	{
			if (isset($_SESSION['JID']))
	{
		$userID=$_SESSION['JID'];
		$currentID=$_SESSION['id'];
		if ($userID!=$currentID)
		{
			$id=$_SESSION['id'];
	
			$publication = "SELECT publications.text, publications.date, publications.numberOfLikes, users.ID, users.FirstName, users.LastName, publications.publisherID, publications.numberOfDislikes, publications.ID  FROM publications JOIN users ON publications.publisherID=users.ID WHERE publications.userID = $id order by publications.ID desc";
			$publication= mysql_query($publication);
			//$idPublication = "SELECT ID FROM publications WHERE currentID = $id";
			//$idPublication = mysql_query($idPublication);
			while($thePublication = mysql_fetch_array($publication, MYSQL_NUM))
			{
				echo ("<br>");
				echo ("<div class='publication'><span class='publisher'><a href='http://sapme.net/Framework/profilePage.php?id=".$thePublication['6']."'>".$thePublication['4']." ".$thePublication['5'].":</a></span><br><p id='parPub'>".$thePublication[0]."</p>  <br><span id='date'>Date: ". $thePublication['1']." </span> &nbsp;&nbsp; <img src = 'like.png' class = 'likeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Like this'> Likes: ". $thePublication['2']."&nbsp;&nbsp;&nbsp; <img src = 'dislike.png' class = 'DislikeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Dislike this'> Dislikes: ".$thePublication['7']."<span class = 'showPublicationCommentsButton' id = '".$thePublication[8]."'>Comments</span>");
				echo ("</div>");			
	
	
			}
	echo "<button id='allPublications'>Hide publications</button>";
		}
	else{
	$id=$_SESSION['id'];
	
	
	$publication = "SELECT publications.text, publications.date, publications.numberOfLikes, users.ID, users.FirstName, users.LastName, publications.publisherID,  publications.numberOfDislikes, publications.ID FROM publications JOIN users ON publications.publisherID=users.ID WHERE publications.userID = $id order by publications.ID desc";
	$publication= mysql_query($publication);
	while($thePublication = mysql_fetch_array($publication, MYSQL_NUM))
	{
		echo ("<br>");
		echo ("<div class='publication' id='".$thePublication[8]."'><span class='publisher'><a href='http://sapme.net/Framework/profilePage.php?id=".$thePublication['6']."'>".$thePublication['4']." ".$thePublication['5'].":</a><img src='Icons/close.png' class='deletePub' id='".$thePublication[8]."'></span><br><p id='parPub'>".$thePublication[0]."</p>  <br><span id='date'>Date: ". $thePublication['1']." </span> &nbsp;&nbsp;<img src = 'like.png' class = 'likeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Like this'> Likes: ". $thePublication['2']."&nbsp;&nbsp;&nbsp; <img src = 'dislike.png' class = 'DislikeButton' id = '".$thePublication['8']."' style = 'cursor:pointer' title = 'Dislike this'> Dislikes: ".$thePublication['7']."<span class = 'showPublicationCommentsButton' id = '".$thePublication[8]."'>Comments</span>");                                                                 
		echo ("</div>");			
	}
	echo "<button id='allPublications'>Hide publications!</button>";
	mysql_query("UPDATE publications SET seen = 1 WHERE userID = {$_SESSION['JID']}");
	}
	echo"<div id='theFiuuuRotateTransformingCommentsDiv' class = 'dialogBody'>
			<div class = 'dialogHeader'>
				<h3>Comments</h3>
				<img src = 'Icons/close.png' class = 'dialogCloseButton' id = 'closePubComment'>
			</div>
			<div class = 'dialogHeaderLess' id = 'theFiuuuRotateTransformingCommentsDivHeaderless'>
			</div>
		</div>";
	}
	else{
		header ('Location: ..\index.php');
		exit();
	}
	}
	?>
		<script>
			$('.deletePub').click(function()
			{
				if(confirm("Are you sure?") == true)
				{
					$('.deletePub').removeClass('focus');
					$(this).addClass('focus');
					var deleteId = $('.focus').attr('id');
					$.post("deletePublication.php", {pubID : deleteId}, function(data)
					{
						if(data == "napsuvai")
						{
							alert("Ne mi sa pravi na mnogo ymen shtot shte ti eba pytkata maina pedal grozen!");
						}
						else
						{
							$("#duvar").load("mainFrame.php");
						}
					});
				}
			});
			$(".likeButton").click(function()
			{
				$(".likeButton").removeClass("focused");
				$(this).addClass("focused");
				var forLikeId = $(".focused").attr("id");
				$.post("likePub.php", {pubID : forLikeId}, function(data)
				{
					if(data == "napsuvai")
					{
						alert("Ne mi sa pravi na mnogo ymen shtot shte ti eba pytkata maina pedal grozen!");
					}
					else
					if(data == "Haresano")
					{
						alert("You have already liked this publication.");
					}
					else
					if(data == "fr")
					{
						alert("You can't like this publication.");
					}
					else
					{
						$("#duvar").load("mainFrame.php");
					}
				});
			});
			$(".dislikeButton").click(function()
			{
				$(".dislikeButton").removeClass("Thisfocused");
				$(this).addClass("Thisfocused");
				var forLikeId = $(".Thisfocused").attr("id");
				$.post("dislikePub.php", {pubID : forLikeId}, function(data)
				{
					if(data == "napsuvai")
					{
						alert("Ne mi sa pravi na mnogo ymen shtot shte ti eba pytkata maina pedal grozen!");
					}
					else
					if(data == "Haresano")
					{
						alert("You have already disliked this publication.");
					}
					else
					if(data == "fr")
					{
						alert("You can't dislike this publication.");
					}
					else
					{
						$("#duvar").load("mainFrame.php");
					}
				});
			});
			function checkIfThereIsNoNewPublication()
			{
				$.post("checkIfPostsAreHuiOrDrNeshto.php", function(data)
				{	
					if(data != "HUI")
					{
						$.post("eventsChecker.php", function(theData)
						{
							$("#duvar").prepend(theData);
						});
					}
				});
			}
			setInterval("checkIfThereIsNoNewPublication()", 43000);
			//show all publications
			
			$("#allPublications").click(function()
			{	var kuche=<?=$all?>;
				if(kuche==0)
				{
					kuche=1;
					$.post("mainFrame.php", {zaek:kuche},function(zele)
					{
						$("#duvar").html(zele);
					});
					
				}	
				else
				{
					kuche=0;
					$.post("mainFrame.php", {zaek:kuche},function(morkovi)
					{
						$("#duvar").html(morkovi);
					});
				}
			});	
				//neshto
	
		</script>
		</body>
</html>	




























	